package com.dd.ent.car.ui.home.templet.video;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.LinearInterpolator;
import android.view.animation.RotateAnimation;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.dd.ent.car.R;
import com.dd.ent.car.common.bean.video.Video;
import com.dd.ent.car.common.http.ImageLoaderUtil;
import com.dd.ent.car.ui.widget.VideoView;

/**
 * Created by aqu-spring on 16/7/6.
 * 视频容器,包含视频的添加,加载,以及视频各种状态的处理
 */
public class VideoItemView extends FrameLayout {
    private ImageView poster;
    private RotateAnimation mRotateAnimation;
    private FrameLayout playContainer;
    private TextView texVideoTitle;

    public VideoItemView(Context context) {
        super(context);
        init(context);
    }

    public VideoItemView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    private void init(Context context) {
        View root = LayoutInflater.from(context).inflate(R.layout.video_item_view, this, true);
        poster = (ImageView) root.findViewById(R.id.poster);
        texVideoTitle = (TextView) root.findViewById(R.id.video_title);
        playContainer = (FrameLayout) root.findViewById(R.id.play_container);
        mRotateAnimation = new RotateAnimation(0f, 359f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        mRotateAnimation.setInterpolator(new LinearInterpolator());
        mRotateAnimation.setDuration(1000);
        mRotateAnimation.setRepeatCount(Animation.INFINITE);

    }

    public void applayVideoView(VideoView videoView) {
        if (videoView == null) return;
        ViewGroup viewGroup = (ViewGroup) videoView.getParent();
        if (viewGroup != null) {
            viewGroup.removeAllViews();
        }
        FrameLayout videoFramLayout = (FrameLayout) findViewById(R.id.play_container);
        if (videoFramLayout != null) {
            videoView.resetData();
            videoFramLayout.addView(videoView);
        }
    }

    public FrameLayout getPlayContainer() {
        return playContainer;
    }

    public void refreshData(int position, Video item) {
        if (poster != null && item != null) {
            ImageLoaderUtil.displayPosterImage(item.getPosterUrl(), poster);
        }
        if (texVideoTitle != null && item != null) {
            texVideoTitle.setText(item.getTitle());
        }
    }
}
